package com.hjq.fcloud.ui.activity

import android.content.Context
import android.content.Intent
import android.text.TextUtils
import android.view.View
import com.hjq.fcloud.R
import com.hjq.fcloud.bean.FlowDataBean
import com.hjq.fcloud.bean.MettingData
import com.hjq.fcloud.event.EventBean
import com.hjq.fcloud.event.EventBusUtils
import com.hjq.fcloud.http.api.IntercreditorAgreementApi
import com.hjq.fcloud.http.api.IntercreditorDetailApi
import com.hjq.fcloud.http.model.HttpData
import com.hjq.fcloud.jump.CommonJumpBean
import com.hjq.fcloud.ktx.ViewKtx.setVisibility
import com.hjq.fcloud.other.AppFilesHelper
import com.hjq.fcloud.other.SignRefershHelper
import com.hjq.http.EasyHttp
import com.hjq.http.body.JsonBody
import com.hjq.http.listener.HttpCallback
import kotlinx.android.synthetic.main.activity_intercreditor_agreement.bidPackageSb
import kotlinx.android.synthetic.main.activity_intercreditor_agreement.feedbackView
import kotlinx.android.synthetic.main.activity_intercreditor_agreement.moneySb
import kotlinx.android.synthetic.main.activity_intercreditor_agreement.nameSb
import kotlinx.android.synthetic.main.activity_intercreditor_agreement.numberSb
import kotlinx.android.synthetic.main.activity_intercreditor_agreement.fileView
import kotlinx.android.synthetic.main.activity_intercreditor_agreement.bottomView
import kotlinx.android.synthetic.main.activity_intercreditor_agreement.feedbackRootView
import kotlinx.android.synthetic.main.activity_intercreditor_agreement.flowView
import kotlinx.android.synthetic.main.activity_intercreditor_agreement.statusLayout
import okhttp3.Call
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**
 * 债权协议会签
 */
class IntercreditorAgreementActivity : BaseDetailActivity() {

    companion object {
        //会签结果 0不通过1通过
        const val STATUS_PASS = "1"
        const val STATUS_NOT_PASS = "0"
        fun start(ac: Context, jumpBean: CommonJumpBean) {
            var intent = Intent(ac, IntercreditorAgreementActivity::class.java)
            intent.putExtra(INTENT_IN_ITEM, jumpBean)
            ac.startActivity(intent)
        }
    }

    private var status:String = STATUS_PASS

    override fun userEventBus(): Boolean {
        return true
    }

    override fun getStateLayout(): View? {
        return statusLayout
    }

    override fun getLayoutId(): Int {
        return R.layout.activity_intercreditor_agreement
    }

    override fun initView() {

    }

    override fun initData() {
        getServerDetail()

        getServerFlow()
    }

    override fun setFlowViewData(data: FlowDataBean?) {
//        if (canSignFromList) {
//            flowView.setVisibility(false)
//
//            //审核控件设置
//            feedbackRootView.setVisibility(true)
//            bottomView.setVisibility(true)
//        } else {
//            flowView.setVisibility(true)
//            flowView.setViewData(data?.setup_list)
//        }
    }


    fun handlerReject(view: View) {
        status = STATUS_NOT_PASS
        if(checkInput()){
            FaceDetectRGBActivity.launcher(context, intent.extras)
        }
    }

    fun handlerConfirm(view: View) {
        status = STATUS_PASS
        if(checkInput()){
            FaceDetectRGBActivity.launcher(context, intent.extras)
        }
    }

    private fun checkInput(): Boolean {
        if (TextUtils.isEmpty(feedbackView.getInPutText())) {
            showToast("请输入意见反馈")
            return false
        }
        return true
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onMainEventBus(bean: EventBean) {
        if (bean != null && bean.type == EventBusUtils.TYPE_FACE_AND_SIGN_FINISH) {
            var parms = bean.parms as HashMap<String, String?>
            requestServerConfirm(parms)
        }
    }

    private fun getServerDetail() {
        showAppLoading()
        var parms = HashMap<String, String?>()
        parms.put("id",commonJumpBean?.bizId)
        EasyHttp.post(this)
            .api(IntercreditorDetailApi())
            .body(JsonBody(parms))
            .request(object : HttpCallback<HttpData<IntercreditorDetailApi.Data?>?>(this) {
                override fun onStart(call: Call?) {}
                override fun onSucceed(data: HttpData<IntercreditorDetailApi.Data?>?) {
                    setViewData(data?.getData())
                }

                override fun onEnd(call: Call?) {
                    super.onEnd(call)
                    showAppContent()
                }
            })
    }

    private fun setViewData(data: IntercreditorDetailApi.Data?) {
        numberSb.setRightText(data?.code)
        nameSb.setRightText(data?.project?.project_name)
        bidPackageSb.setRightText(data?.sect?.sect_name)
        moneySb.setRightText(data?.money)

        fileView.setViewData(this,"债权凭证", AppFilesHelper.getAllFiles(data?.files))
    }

    private fun requestServerConfirm(signParms: HashMap<String, String?>) {
        var parms = HashMap<String, String?>()
        if(signParms != null){
            parms.putAll(signParms)
        }
        parms.put("reason",feedbackView.getInPutText())
        parms.put("result",status)
        parms.put("biz_code",commonJumpBean?.bizCode)
        parms.put("biz_id",commonJumpBean?.bizId)
        EasyHttp.post(this)
            .api(IntercreditorAgreementApi())
            .body(JsonBody(parms))
            .request(object : HttpCallback<HttpData<MettingData?>?>(this) {
                override fun onSucceed(data: HttpData<MettingData?>?) {
                    SignRefershHelper.signSuccessRefersh(context,commonJumpBean)
                    showToast(data?.getMessage())
                }
            })
    }
}